layui.use(['form','tree','element','table','laytpl','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    var request = GetRequest();


    var option = {
        elem: '#tableList',
        url : '/log/analysejob/condition/query',
        cellMinWidth : 95,
        page : true,
        height : "full-135",
        limits : [10,15,20,25],
        limit : 10,
        id : "listTable",
        where:{jobid:request.jobid},
        /*toolbar: true,
        totalRow: true,*/
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'jobid', title: '任务编号', minWidth:200, align:"center"},
            {field: 'field', title: '字段名', minWidth:200, align:"center"},
            {field: 'value', title: '值', align:'center'},
            {field: 'compare', title: '过滤等级', align:'center',templet:function(d){
                    return d.compare == 1 ? '包含':'等于';
                }},
            {field: 'createuser', title: '创建用户', align:'center'},
            {field: 'updateuser', title: '更新用户',  align:'center'},
            {field: 'createdate', minWidth:120, title: '添加时间', sort: true,align:'center',templet:function(d){
                    //处理时间
                    var date = new Date(d.createdate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }},
            {title: '操作', minWidth:175,fixed:"right", templet:'#tableListBar',align:"center"}
        ]]
    };

    //日志分析过滤规则任务列表
    var tableIns = table.render(option);

    //搜索日志分析过滤规则任务
    $(".search_btn").on("click",function() {
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '';
            option.where = {};
            table.render(option);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加日志分析过滤规则任务
    $(".addBtn").click(function(){
        var index = layui.layer.open({
            title : "添加日志分析过滤规则任务",
            type : 2,
            area: ['600px','300px'],
            content : "analysejobcondition_add.html?jobid=" + request.jobid,
            success : function(layero, index){
                /*setTimeout(function(){
                    layui.layer.tips('点击此处返回日志分析过滤规则任务列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)*/
            }
        });
        /*layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })*/
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的日志分析过滤规则任务？', {icon: 3, title: '提示信息'}, function (index) {
                for(var i=0;i<idArr.length;i++){
                    $.get("/log/analysejob/condition/delete?id=" + idArr[i],function(data){
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }
            })
        }else{
            layer.msg("请选择需要删除的日志分析过滤规则任务");
        }
    });


    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit') { //编辑
            var index = layui.layer.open({
                title : "修改日志分析过滤规则任务信息",
                type : 2,
                area: ['600px','300px'],
                content : "analysejobcondition_edit.html?id=" + data.id,
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回日志分析过滤规则任务列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此日志分析过滤规则任务？',{icon:3, title:'提示信息'},function(index){
                $.get("/log/analysejob/condition/delete?id=" + data.id,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});